
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>列表</title>
    {include file="include/top"}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            {include file="include/selectsearch" ctype='layui-inline' title="分类" classname='cids' optionHtml='<?php echo isset($cids)?$cids:"";?>' }
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="请填入..." autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
            </div>
        </div>
        <div class="layui-card-header layuiadmin-card-header-auto">
            <button type="button" class="layui-btn" id="test-upload-type1" data-imgid="addfood"><i class="layui-icon"></i>添加</button>
        </div>
        <table class="layui-table" lay-data="{height:0, url:'/service/listGet', page:true, id:'idTest',toolbar: '#toolbarDemo'}" lay-filter="demo">
            <thead>
            <tr>
                <th lay-data="{field:'id',  sort: true, fixed: true,width:80,checkbox:true}">ID</th>
                <th lay-data="{field:'id',  sort: true, fixed: true,width:80}">ID</th>
                <th lay-data="{field:'cids'}">分类</th>
                <th lay-data="{field:'tipsID'}">标签</th>
                <th lay-data="{field:'title'}">名称</th>
                <th lay-data="{field:'mobile'}">电话</th>
                <th lay-data="{field:'contacts'}">联系人</th>
                <th lay-data="{field:'status',templet:function(d){
                    if(d.status==0)
                        return '正常';
                    else
                        return '暂停';
                    }
                }
                ">状态</th>
                <th lay-data="{field:'amount'}">余额</th>
                <th lay-data="{field:'subPercent',edit:true}">让利%✎</th>
                <th lay-data="{field:'subPrice',edit:true}">让利额✎</th>
                <th lay-data="{fixed: 'right',  align:'center', toolbar: '#barDemo',width:80}"></th>
            </tr>
            </thead>
        </table>
        <script type="text/html" id="toolbarDemo">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="tipsSave">保存到标签</button>
            {include file="include/selectsearch" ctype='layui-inline' title="标签" classname='tips' optionHtml='<?php echo isset($tips)?$tips:"";?>' }
        </script>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="more">操作<i
                    class="layui-icon layui-icon-down"></i></a>
        </script>
    </div>
</div>
{include file="include/js"}
<script>
    var pathid=0;
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table', 'dropdown'], function(){
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var dropdown = layui.dropdown
        var pathid=0;
        var id=0;
        $("#test-upload-type1").click(function (){
            id=0;
            add();
        })
        $(document).on("click","#tipsSave",function () {
            var $tipsID = $("select[name='tips']").val()
            var checkIds = []
            var selectData = table.checkStatus('idTest');
            if (selectData.data.length > 0 && $tipsID > 0) {
                selectData.data.forEach(res => {
                    checkIds.push(res.id)
                })
                layer.confirm("是否确定保存？", function () {
                    $.post("tipsSaveIDS", {
                        tipsID: $tipsID,
                        checkIds: JSON.stringify(checkIds)
                    }, function (res) {
                        if (res.code == 0) {
                            layer.closeAll()
                            layer.msg(res.msg)
                        } else {
                            layer.msg(res.msg)
                        }
                    })
                })
            } else {
                layer.msg("选择标签和服务商")
            }
        })
        table.on("edit(demo)",function (re){
            var $field = re.field
            var $value =  re.value
            var $id =  re.data.id
            var obj=$(this)
            if( $field == "subPrice" || $field=='subPercent'){
                //设置整单优惠
                $.post("listSave", {id: $id,value:$value,field:$field}, function (res) {
                    layer.tips(res.msg,obj)
                })
            }
        })
        function add(){
            layer.open({
                type: 2
                ,title: '添加'
                ,content: 'listAdd?id='+id
                ,btn: ['确定', '取消']
                ,area: ['80%', '80%']
                ,yes: function(index, layero){
                    layer.confirm('是否确定操作?', {icon: 3, title:'提示'}, function(index){
                        //do something
                        var $data={};
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags");
                        $data.cids = othis.find('select[name="cids"]').val()
                        $data.status = othis.find('select[name="status"]').val()
                        $data.payType = othis.find('select[name="payType"]').val()
                        $data.title = othis.find('input[name="title"]').val()
                        $data.mobile = othis.find('input[name="mobile"]').val()
                        $data.contacts = othis.find('input[name="contacts"]').val()
                        $data.subPercent = othis.find('input[name="subPercent"]').val()
                        $data.subPrice = othis.find('input[name="subPrice"]').val()
                        $data.username = othis.find('input[name="username"]').val()
                        $data.key = othis.find('input[name="key"]').val()
                        $data.urlGas = othis.find('input[name="urlGas"]').val()
                        $data.urlOrder = othis.find('input[name="urlOrder"]').val()
                        $data.content = othis.find('#LAY_layedit_2').contents().find("body").html()

                        $.post("listSave?id="+id,$data,function (re) {
                            layer.msg(re.msg);
                            if(re.code == 0){
                                setTimeout(function () {
                                    location.reload();
                                },2000);
                            }
                        },'json');
                        layer.close(index);
                    });
                }
            });
        }

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('idTest', {
                where: field
            });
        });
        $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        function rechargeAdmin2(){
            layer.open({
                type: 2
                ,title: '手动充值'
                ,content: 'rechargeAdmin2?userID='+id+"&id=0"
                ,btn: ['确定', '取消']
                ,area: ['40%', '60%']
                ,yes: function(index, layero){
                    layer.confirm('是否确定操作?', {icon: 3, title:'提示'}, function(index){
                        //do something
                        var data={}
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags");
                        data.amount = othis.find('input[name="amount"]').val()
                        data.img = othis.find('input[name="img"]').val()
                        data.pays = othis.find('select[name="pays"]').val()
                        $.post("rechargeAdmin2Save?userID="+id,data,function (re) {
                            layer.msg(re.msg);
                        },'json');
                        layer.closeAll();
                    });
                }
            });
        }
        function setDisable(obj,menuStr){
            if(menuStr=='启用'){
                layer.confirm("是否确定启用？",function (){
                    $.post("listSave", {id: id,value:0,field:'status'}, function (res) {
                        layer.closeAll()
                        layer.msg(res.msg,obj)
                    })
                })
            }else{
                layer.prompt("请输入禁用原因：",function (re){
                    $.post("listSave", {id: id,value:-1,field:'status',content:re}, function (res) {
                        layer.closeAll()
                        layer.msg(res.msg,obj)
                    })
                })
            }
        }
        function setGas(){
            layer.open({
                type: 2
                ,title: '服务商指定加油站'
                ,content: 'setGas?id='+id
                ,btn: ['确定', '取消']
                ,area: ['100%', '80%']
                ,yes: function(index, layero){
                    layer.confirm('是否确定修改?', {icon: 3, title:'提示'}, function(index){
                        //do something
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                            ,inGasTips = othis.find('input[name="inGasTips"]').val()
                        $.post("setGasSave",{inGasTips:inGasTips,serviceID:id},function (re) {
                            layer.msg(re.msg);
                            setTimeout(function () {
                                location.reload();
                            },2000);
                        },'json');
                        layer.close(index);
                    });
                }
            });
        }
        //监听工具条
        table.on('tool(demo)', function(obj){

            var that = this
            var data = obj.data;
            if (obj.event === 'more') {
                //更多下拉菜单
                var thatData = data
                var menuStr="禁用";
                if(thatData.status==-1){
                    menuStr = '启用'
                }
                dropdown.render({
                    elem: that
                    , show: true //外部事件触发即显示
                    , data: [{
                        title: '编辑信息'
                        , id: 'gasEdit'
                    },{
                        title: '充值'
                        , id: 'addTail'
                    },{
                        title: '指定油站'
                        , id: 'setGas'
                    },{
                        title: menuStr
                        , id: 'setDisable'
                    },
                    ]
                    , click: function (data, othis) {
                        //根据 id 做出不同操作
                        if (data.id === 'setGas') {
                            id = thatData.id
                            setGas();
                        }else if(data.id == 'gasEdit'){
                            id = thatData.id
                            add();
                        }else if(data.id == 'addTail'){
                            id = thatData.id
                            rechargeAdmin2();
                        }else if(data.id == 'guns'){
                            id = thatData.id
                            guns();
                        }else if(data.id == 'setDisable'){
                            id = thatData.id
                            setDisable(obj,menuStr);
                        }
                    }
                    , align: 'right' //右对齐弹出（v2.6.8 新增）
                    , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
                });
            }
        });
    });
</script>

</body>
</html>
